//
//  CAShapeLayer+DashSeperator.m
//  weidianguanjia
//
//  Created by 谢英泽 on 15/11/20.
//  Copyright © 2015年 谢英泽. All rights reserved.
//

#import "CAShapeLayer+DashSeperator.h"


@implementation CAShapeLayer (DashSeperator)

+ (instancetype)layerWithFrame:(CGRect)frame
{
    CAShapeLayer *shapeLayer;
    if ((shapeLayer = [CAShapeLayer layer])) {
        [shapeLayer setBounds:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [shapeLayer setPosition: CGPointMake(CGRectGetMidX(frame),CGRectGetMidY(frame))];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [shapeLayer setStrokeColor:[kCOLOR_line CGColor]];// 设置虚线颜色为blackColor
        [shapeLayer setLineWidth:0.5f];// 0.5f设置虚线的宽度
        [shapeLayer setLineJoin:kCALineJoinRound];
        [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:
                                        [NSNumber numberWithInt:5],
                                        [NSNumber numberWithInt:5],
                                        nil]];// 5=线的宽度 5=每条线的间距
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, 0);
        CGPathAddLineToPoint(path, NULL, frame.size.width,0);
        [shapeLayer setPath:path];
        CGPathRelease(path);
    }
    return shapeLayer;
}

+ (instancetype)layerWithFrame:(CGRect)frame lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer;
    if ((shapeLayer = [CAShapeLayer layer])) {
        [shapeLayer setBounds:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [shapeLayer setPosition: CGPointMake(CGRectGetMidX(frame),CGRectGetMidY(frame))];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
        [shapeLayer setStrokeColor:[lineColor CGColor]];// 设置虚线颜色为blackColor
        [shapeLayer setLineWidth:0.5f];// 0.5f设置虚线的宽度
        [shapeLayer setLineJoin:kCALineJoinRound];
        [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:
                                        [NSNumber numberWithInt:5],
                                        [NSNumber numberWithInt:5],
                                        nil]];// 5=线的宽度 5=每条线的间距
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, 0);
        CGPathAddLineToPoint(path, NULL, frame.size.width,0);
        [shapeLayer setPath:path];
        CGPathRelease(path);
    }
    return shapeLayer;
}

+ (instancetype)layerArrowWithPoint:(CGPoint)point
{
    CAShapeLayer *shapeLayer;
    if ((shapeLayer = [CAShapeLayer layer])) {
        shapeLayer.frame = CGRectMake(point.x, point.y, 6, 12);
        shapeLayer.strokeColor = kCOLOR_line.CGColor;
        shapeLayer.fillColor = kCOLOR_clear.CGColor;
        shapeLayer.lineWidth = 1.5;
        shapeLayer.lineCap = kCALineCapSquare;
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, 0);
        CGPathAddLineToPoint(path, NULL, shapeLayer.frame.size.width, shapeLayer.frame.size.height/2);
        CGPathAddLineToPoint(path, NULL, 0, shapeLayer.frame.size.height);

        [shapeLayer setPath:path];
        CGPathRelease(path);
    }
    return shapeLayer;
}

+ (instancetype)layerLineWithFrame:(CGRect)frame
{
    CAShapeLayer *shapeLayer;
    [shapeLayer setBackgroundColor:kCOLOR_clear.CGColor];
    if ((shapeLayer = [CAShapeLayer layer])) {
        shapeLayer.frame = frame;
        shapeLayer.strokeColor = kCOLOR_line.CGColor;
        shapeLayer.fillColor = kCOLOR_clear.CGColor;
        shapeLayer.lineWidth = 0.3;
        shapeLayer.lineCap = kCALineCapSquare;
        
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 0, 0);
        //垂直
        if (frame.size.width < frame.size.height) {
            CGPathAddLineToPoint(path, NULL, 0, shapeLayer.frame.size.height);
        }else{
            //水平
            CGPathAddLineToPoint(path, NULL, shapeLayer.frame.size.width, kUI_Zero);
        }
        
        [shapeLayer setPath:path];
        CGPathRelease(path);
    }
    return shapeLayer;
}

@end
